• Home
  • Introduction
  • Data Source
  • Data Visualization
  • Exploratory Data Analysis
  • ARMA/ARIMA/SARIMA Model
  • ARIMAX Model
  • Financial Time Series Model
  • Deep Learning for TS
  • Conclusion

EDA for Communication Services Sector Fund

The Communication Services Sector Fund (XLC) is an exchange-traded fund (ETF) that seeks to track the performance of companies in the communication services sector of the S&P 500 index. The fund’s holdings include companies such as Alphabet Inc., Facebook Inc., and Verizon Communications Inc. The communication services sector is a relatively new sector, created in 2018, and encompasses companies that provide communication services such as telecommunication, media, and entertainment. The XLC ETF provides investors with exposure to the growth potential of these companies, which are often at the forefront of technological innovation and changing consumer habits. The fund is relatively diversified, with holdings across different sub-sectors within communication services. As with any ETF, the XLC fund provides investors with the ability to invest in a portfolio of stocks with a single trade, making it a convenient option for those seeking exposure to the communication services sector.

Time Series Plot
Code
# get data
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)


data = getSymbols("XLC",src='yahoo', from = '2018-06-19',to = "2023-03-01")

df <- data.frame(Date=index(XLC),coredata(XLC))

# create Bollinger Bands
bbands <- BBands(XLC[,c("XLC.High","XLC.Low","XLC.Close")])

# join and subset data
df <- subset(cbind(df, data.frame(bbands[,1:3])), Date >= "2018-06-19")

#export the data 
xlc_data <- df
write.csv(xlc_data, "DATA/CLEANED DATA/xlc_raw_data.csv", row.names=FALSE)

# colors column for increasing and decreasing
for (i in 1:length(df[,1])) {
  if (df$XLC.Close[i] >= df$XLC.Open[i]) {
      df$direction[i] = 'Increasing'
  } else {
      df$direction[i] = 'Decreasing'
  }
}

i <- list(line = list(color = '#6F9860'))
d <- list(line = list(color = '#7F7F7F'))

# plot candlestick chart

fig <- df %>% plot_ly(x = ~Date, type="candlestick",
          open = ~XLC.Open, close = ~XLC.Close,
          high = ~XLC.High, low = ~XLC.Low, name = "XLC",
          increasing = i, decreasing = d) 
fig <- fig %>% add_lines(x = ~Date, y = ~up , name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands",
            hoverinfo = "none", inherit = F) 
fig <- fig %>% add_lines(x = ~Date, y = ~dn, name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands", inherit = F,
            showlegend = FALSE, hoverinfo = "none") 
fig <- fig %>% add_lines(x = ~Date, y = ~mavg, name = "Mv Avg",
            line = list(color = '#E377C2', width = 0.5),
            hoverinfo = "none", inherit = F) 
fig <- fig %>% layout(yaxis = list(title = "Price"))

# plot volume bar chart
fig2 <- df 
fig2 <- fig2 %>% plot_ly(x=~Date, y=~XLC.Volume, type='bar', name = "XLC Volume",
          color = ~direction, colors = c('#6F9860','#7F7F7F')) 
fig2 <- fig2 %>% layout(yaxis = list(title = "Volume"))

# create rangeselector buttons
rs <- list(visible = TRUE, x = 0.5, y = -0.055,
           xanchor = 'center', yref = 'paper',
           font = list(size = 9),
           buttons = list(
             list(count=1,
                  label='RESET',
                  step='all'),
             list(count=3,
                  label='3 YR',
                  step='year',
                  stepmode='backward'),
             list(count=1,
                  label='1 YR',
                  step='year',
                  stepmode='backward'),
             list(count=1,
                  label='1 MO',
                  step='month',
                  stepmode='backward')
           ))

# subplot with shared x axis
fig <- subplot(fig, fig2, heights = c(0.7,0.2), nrows=2,
             shareX = TRUE, titleY = TRUE)
fig <- fig %>% layout(title = paste("Communication Services Sector Fund Stock Price: June 2018 - March 2023"),
         xaxis = list(rangeselector = rs),
         legend = list(orientation = 'h', x = 0.5, y = 1,
                       xanchor = 'center', yref = 'paper',
                       font = list(size = 10),
                       bgcolor = 'transparent'))

fig

Since its inception in June 2018, the Communication Services Sector Fund (XLC) has exhibited both upward and downward trends in its stock price. The fund started off on a positive note, rising steadily in 2018 and reaching new highs by the end of the year. However, like most other stocks and funds, it experienced a significant decline in value in early 2020 due to the COVID-19 pandemic, losing over 33% of its value in just a few weeks.

The XLC fund then regained some of its losses, aided by government stimulus measures and low-interest rates, and continued its upward trend throughout 2020 and into 2021. However, the fund experienced some volatility during the latter part of 2021 and early 2022, with a few significant dips in its stock price. This volatility can be attributed to various factors such as rising interest rates, concerns about inflation, and geopolitical tensions.

Overall, the Communication Services Sector Fund’s stock price has exhibited significant fluctuations since its inception in 2018, with multiple peaks and troughs. However, despite the fluctuations, the fund has exhibited an overall upward trend, reaching new all-time highs multiple times over the period. The fund’s trend is reflective of the growth potential of the communication services sector, which continues to evolve rapidly as new technologies and changing consumer habits drive innovation and demand for new services.

For stock prices, a multiplicative decomposition is typically preferred because the percentage changes in stock prices tend to be more important than the absolute changes. Additionally, stock prices tend to exhibit non-constant variance, meaning that the variance of the series changes over time. A multiplicative decomposition can handle this non-constant variance more effectively than an additive decomposition.

Decomposed Time Series

  • Decomposition Plot
  • Adjusted Decomposition Plot
Code
#time series data
myts<-ts(df$XLC.Adjusted,frequency=252,start=c(2018,6,19), end = c(2023,3,1)) 
#original plot for time series data
orginial_plot <- autoplot(myts,xlab ="Year", ylab = "Adjusted Closing Price", main = "Communication Services Sector Fund Stock price: June 2018 - March 2023")
#decompose the data
decompose = decompose(myts, "multiplicative")
#decomposition plot
autoplot(decompose)

Code
#adjusted plot
trendadj <- myts/decompose$trend
decompose_adjtrend_plot <- autoplot(trendadj,ylab='trend') +ggtitle('Adjusted trend component in the multiplicative time series model')
seasonaladj <- myts/decompose$seasonal
decompose_adjseasonal_plot <- autoplot(seasonaladj,ylab='seasonal') +ggtitle('Adjusted seasonal component in the multiplicative time series model')
grid.arrange(orginial_plot, decompose_adjtrend_plot,decompose_adjseasonal_plot, nrow=3)

The adjusted seasonal component tend to have upward trend and there is more variability in the model when compared to the original plot where the variation during the years but the adjusted trend then to have more fluctuation showing no trend when compared to the original plot.

Lag Plots

  • Daily Time Lags
  • Monthly Time Lags
Code
#Lag plots 
gglagplot(myts, do.lines=FALSE, lags=1)+xlab("Lag 1")+ylab("Yi")+ggtitle("Lag Plot for Communication Services Sector Fund Stock June 2018 - March 2023")

Code
#montly data
mean_data <- df %>% 
  mutate(month = month(Date), year = year(Date)) %>% 
  group_by(year, month) %>% 
  summarize(mean_value = mean(XLC.Adjusted))
month<-ts(mean_data$mean_value,start = c(2018, 6),frequency = 12)
#Lag plot
ts_lags(month)

The first lag plot shows the daily time lags of the Communication Services Sector Fund stock price from June 2018 to March 2023. The plot indicates that there is a strong positive correlation between the current value and the previous day’s value, as seen by the points clustering along the diagonal line. This suggests that the stock price has a positive autocorrelation at a lag of one day.

The second lag plot shows the monthly time lags of the mean value of the Communication Services Sector Fund stock price from June 2018 to March 2023. The plot indicates that there is a positive correlation between the current value and the value from the previous month. This suggests that the mean value of the stock price has a positive autocorrelation at a lag of one month.

Overall, the lag plots indicate that there is a positive autocorrelation present in the Communication Services Sector Fund stock price data, with the strongest correlation observed in the daily time series.

Seasonality

  • Seasonal Heatmap
  • Seasonal Line plot
Code
# Create seasonal plot
ts_heatmap(month, color = "PuRd", title = 'Seasonality Heatmap of Communication Services Sector Fund Stock Jan 2018 - March 2023')
Code
# Create a line graph for each year with months on the x-axis
ggseasonplot(month, datecol = "date", valuecol = "value")+ggtitle("Seasonal Yearly Plot for Communication Services Sector Fund Stock Jan 2018 - March 2023")

The Seasonality Heatmap for the Communication Services Sector Fund Stock June 2018 - March 2023 does not reveal any clear seasonality in the data. The heatmap shows the mean value of the time series for each month and year combination, with the darker colors indicating higher values. The lack of clear patterns or darker colors in specific months or years suggests that there is no consistent seasonal pattern in the data. However, the yearly line graph shows a slight upward trend in the stock price from 2018 to 2023, but does not show any clear seasonality. Each year’s data is represented by a line, and the months are plotted on the x-axis. Overall, the lack of clear seasonality in both the heatmap and yearly line graph suggests that other factors beyond seasonality are driving the stock price fluctuations.

Moving Average

  • 4 Month MA
  • 1 Year MA
  • 3 Year MA
Code
#SMA Smoothing 
ma <- autoplot(month, series="Data") +
  autolayer(ma(month,5), series="4 Month MA") +
  xlab("Year") + ylab("GWh") +
  ggtitle("Communication Services Sector Fund Stock June 2018 - March 2023(4 Month Moving Average)") +
  scale_colour_manual(values=c("Data"="grey50","4 Month MA"="red"),
                      breaks=c("Data","4 Month MA"))
ma

Code
#SMA Smoothing 
ma <- autoplot(month, series="Data") +
  autolayer(ma(month,13), series="1 Year MA") +
  xlab("Year") + ylab("GWh") +
  ggtitle("Communication Services Sector Fund Stock June 2018 - March 2023(1 Year Moving Average)") +
  scale_colour_manual(values=c("Data"="grey50","1 Year MA"="red"),
                      breaks=c("Data","1 Year MA"))
ma

Code
#SMA Smoothing 
ma <- autoplot(month, series="Data") +
  autolayer(ma(month,37), series="3 Year MA") +
  xlab("Year") + ylab("GWh") +
  ggtitle("Communication Services Sector Fund Stock June 2018 - March 2023(3 Year Moving Average)") +
  scale_colour_manual(values=c("Data"="grey50","3 Year MA"="red"),
                      breaks=c("Data","3 Year MA"))
ma

The three plots show the Communication Services Sector Fund stock prices from June 2018 to March 2023, along with the moving averages for 4 months, 1 year and 3 years. As the window of the moving average increases, the smoother the trend line becomes, reducing the impact of noise and fluctuations in the original time series.

The 4-month moving average plot shows frequent fluctuations in the stock price, with the trend line following the general direction of the time series. The 1-year moving average plot shows a smoother trend, following the overall upward trend of the stock price.

The 1-year moving average plot shows a similar trend to the 4-month plot but is even smoother, with fewer fluctuations. Finally, the 3-year moving average plot shows the smoothest trend, with an almost constant upward slope.As the moving average window increases, the smoother trend allows for a clearer identification of the general trend of the Communication Services Sector Fund stock prices over time. From the moving average obtained above we can see that there is upward tend in the stock price of Communication Services Sector Fund.

Autocorrelation Time Series

  • ACF
  • PACF
  • ADF Test
Code
#ACF for  data
ggAcf(month)+ggtitle("ACF Plot for Communication Services Sector Fund Stock June 2018 - March 2023")

Code
#PACF for data
ggPacf(month)+ggtitle("PACF Plot for Communication Services Sector Fund Stock June 2018 - March 2023")

Code
#check the stationarity
tseries::adf.test(month)

    Augmented Dickey-Fuller Test

data:  month
Dickey-Fuller = -1.6946, Lag order = 3, p-value = 0.6979
alternative hypothesis: stationary

In the plot of autocorrelation function, which is the acf graph for monthly data, there are clear autocorrelation in lag. The above lag plots and autocorrelation plot indicates seasonality in the series, which means the series is not stationary.. It was also verified using Augmented Dickey-Fuller Test which tells us that as the p value is greater than 0.05, the series is not stationary.

Detrend and Differenced Time Series

  • Linear Fitting Model
  • ACF Plot
Code
fit = lm(myts~time(myts), na.action=NULL) 
summary(fit) 

Call:
lm(formula = myts ~ time(myts), na.action = NULL)

Residuals:
    Min      1Q  Median      3Q     Max 
-19.055  -6.857  -2.895   7.079  25.301 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) -6478.0495   435.9097  -14.86   <2e-16 ***
time(myts)      3.2341     0.2157   14.99   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 11.03 on 1256 degrees of freedom
Multiple R-squared:  0.1518,    Adjusted R-squared:  0.1511 
F-statistic: 224.7 on 1 and 1256 DF,  p-value: < 2.2e-16
Code
# plot ACFs
plot1 <- ggAcf(myts, 48, main="Original Data: Communication Services Sector Fund Stock Stock Price")
plot2 <- ggAcf(resid(fit), 48, main="Detrended data")
plot3 <- ggAcf(diff(myts), 48, main="First differenced data")
grid.arrange(plot1, plot2, plot3, nrow=3)

The estimated slope coefficient β1, 0.2562. With a standard error of 0.1795, yielding a significant estimated increase of stock price is very less yearly. Equation of the fit for stationary process: \[\hat{y}_{t} = x_{t}+(462.6703)-(0.2562)t\]

From the above graph we can say that there is no change in detrended plot and the original data acf plot, it typically means that the data is stationary. But when the first order difference is applied the high correlation is removed but there is no seasonal correlation.

As depicted in the above figure, the series is now stationary and ready for future study.